package com.ruoyi.web.controller.business;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtPatientReport;
import com.ruoyi.business.service.IHurtPatientReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 患者上传报告Controller
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/business/report")
public class HurtPatientReportController extends BaseController
{
    @Autowired
    private IHurtPatientReportService hurtPatientReportService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询患者上传报告列表
     */
    @PreAuthorize("@ss.hasPermi('business:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtPatientReport hurtPatientReport)
    {
        startPage();
        List<HurtPatientReport> list = hurtPatientReportService.selectHurtPatientReportList(hurtPatientReport);
        return getDataTable(list);
    }

    /**
     * 导出患者上传报告列表
     */
    @PreAuthorize("@ss.hasPermi('business:report:export')")
    @Log(title = "患者上传报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtPatientReport hurtPatientReport)
    {
        List<HurtPatientReport> list = hurtPatientReportService.selectHurtPatientReportList(hurtPatientReport);
        ExcelUtil<HurtPatientReport> util = new ExcelUtil<HurtPatientReport>(HurtPatientReport.class);
        util.exportExcel(response, list, "患者上传报告数据");
    }

    /**
     * 获取患者上传报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hurtPatientReportService.selectHurtPatientReportById(id));
    }

    /**
     * 新增患者上传报告
     */
    @PreAuthorize("@ss.hasPermi('business:report:add')")
    @Log(title = "患者上传报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtPatientReport hurtPatientReport)
    {
        return toAjax(hurtPatientReportService.insertHurtPatientReport(hurtPatientReport));
    }

    /**
     * 修改患者上传报告
     */
    @PreAuthorize("@ss.hasPermi('business:report:edit')")
    @Log(title = "患者上传报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtPatientReport hurtPatientReport)
    {
        return toAjax(hurtPatientReportService.updateHurtPatientReport(hurtPatientReport));
    }

    /**
     * 删除患者上传报告
     */
    @PreAuthorize("@ss.hasPermi('business:report:remove')")
    @Log(title = "患者上传报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hurtPatientReportService.deleteHurtPatientReportByIds(ids));
    }

    /**
     * 上传报告图片
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadReportPicture")
    public AjaxResult uploadReportPicture(@RequestParam("reportPicture") MultipartFile file) throws Exception{
        System.out.println("报告图片=" + file);
        if (!file.isEmpty())
        {
            String report = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            System.out.println("上传成功地址:"+report);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", serverConfig.getUrl()+"/prod-api"+report);
            return ajax;
        }
        return error("上传异常");
    }

    /**
     * 患者提交报告
     */
    @PostMapping("/uploadPatientReport")
    public AjaxResult uploadPatientReport(@RequestBody HurtPatientReport hurtPatientReport){

        Long userId = SecurityUtils.getUserId();
        hurtPatientReport.setPatientId(userId);
        return toAjax(hurtPatientReportService.uploadPatientReport(hurtPatientReport));
    }

    /**
     * 获取患者历史报告
     * @return
     */
    @GetMapping("/getHistoryReport")
    public TableDataInfo getHistoryReport(){
        Long patientId = SecurityUtils.getUserId();
        startPage();
        List<Map<String,Object>> list = hurtPatientReportService.getHistoryReport(patientId);
        for (Map<String,Object> map:list) {
            String report = String.valueOf(map.get("report"));
            String[] split = report.split(",");
            map.put("report", Arrays.asList(split));
        }
        return getDataTable(list);
    }

    /**
     * 医师获取患者所有报告
     * @param patientId
     * @return
     */
    @GetMapping("/getPatientReportList")
    public AjaxResult getPatientReportList(Long patientId){
        List<Map<String, Object>> list = hurtPatientReportService.getHistoryReport(patientId);
        for (Map<String,Object> map:list) {
            String report = String.valueOf(map.get("report"));
            String[] split = report.split(",");
            map.put("report", Arrays.asList(split));
        }
        return AjaxResult.success(list);
    }
}
